package com.liuyucheng.idea_springboot.controller;

import com.liuyucheng.idea_springboot.bean.DataInteraction;
import com.liuyucheng.idea_springboot.bean.LycDemoPo;
import com.liuyucheng.idea_springboot.service.StockPileService;
import com.liuyucheng.idea_springboot.util.PageQuery;
import com.liuyucheng.idea_springboot.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

@RestController
@RequestMapping("stockPile")
public class StockPileController {

    private StockPileService stockPileService;

    /**
     * setter方法注入的写法,setter方法上加上Autowired注解
     * @param stockPileService
     */
    @Autowired
    public void setStockPileService(StockPileService stockPileService) {
        this.stockPileService = stockPileService;
    }

    /**
     * 获取下拉框信息
     */
    @PostMapping("initComboData")
    public ResultUtil<List<Map<String,Object>>> initComboData() {
        System.out.println("进入到initComboData");
        return stockPileService.initComboData();
    }

    @RequestMapping(value = "/table", method = {RequestMethod.POST}, consumes = {MediaType.ALL_VALUE})
    public ResultUtil table(@RequestBody PageQuery<DataInteraction> pageQuery) {
        return ResultUtil.success(stockPileService.table(pageQuery));
    }

    @RequestMapping(value = "/exportExcel")
    public void exportExcel(@RequestBody PageQuery<DataInteraction> pageQuery, HttpServletRequest request, HttpServletResponse response) throws Exception {
        stockPileService.exportExcel(pageQuery, response);
    }

    @RequestMapping(value = "/demoTable", method = {RequestMethod.POST}, consumes = {MediaType.ALL_VALUE})
    public ResultUtil demoTable(@RequestBody PageQuery<LycDemoPo> pageQuery) {
        return ResultUtil.success(stockPileService.demoTable(pageQuery));
    }

    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.POST}, consumes = {MediaType.ALL_VALUE})
    public ResultUtil saveOrUpdate(@RequestBody LycDemoPo po) {
        return ResultUtil.success(stockPileService.saveOrUpdate(po));
    }

    @RequestMapping(value = "/delete", method = {RequestMethod.POST}, consumes = {MediaType.ALL_VALUE})
    public ResultUtil delete(@RequestBody String[] ids) {
        boolean bool = stockPileService.delete(ids);
        ResultUtil<Boolean> result = ResultUtil.success();
        if(bool){
            result.setMessage("删除成功");
        }else{
            result.setCode(-1);
            result.setMessage("删除失败");
        }
        return result;
    }

    @RequestMapping("/uploadFile")
    public @ResponseBody ResultUtil uploadFile(HttpServletRequest request,@RequestParam("file") MultipartFile file) {
        Map<String,String> res = stockPileService.uploadFile(file);
        return ResultUtil.success(res);
    }

    @RequestMapping(value = "/downloadFile")
    public void downloadFile(@RequestParam("fileId") String fileId, HttpServletResponse response) throws Exception {
        System.out.println(fileId);
        stockPileService.downloadFile(fileId, response);
    }

    /**
     * 带附件的表单提交
     * @param files 附件
     * @param energy 其他数据
     * @return
     * @throws Exception
     */
    @PostMapping("/updateProductImg")
    public ResultUtil updateUserImg(@RequestParam("filename") List<MultipartFile> files,
                                    @RequestParam("energy") String energy,
                                    HttpServletRequest request)
            throws Exception{
        //图片保存路径
        String filePath= "C:\\Users\\LiuYucheng\\Downloads";
        List<String> fileName=new ArrayList<>();
        //获取原始图片的拓展名
        for (MultipartFile file: files) {
            String originalFilename = file.getOriginalFilename();
            //新的文件名字
            String newFileName = UUID.randomUUID() + originalFilename;
            //封装上传文件位置的全路径
            File targetFile = new File(filePath, newFileName);
            //把本地文件上传到封装上传文件位置的全路径
            file.transferTo(targetFile);
            fileName.add(newFileName);
        }
        ResultUtil res = new ResultUtil();
        res.setData(fileName);

        //从request中获取表单的其他值
        MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
        Map<String,Object> vo = new HashMap<>();
        vo.put("energy",params.getParameter("energy"));
        vo.put("name",params.getParameter("name"));
        vo.put("age",params.getParameter("age"));
        vo.put("startTime",params.getParameter("startTime"));

        return res;
    }
}
